#include <PA9.h>
#include "gfx/all_gfx.h"
#include "gfx/all_gfx.c"

int main(int argc, char ** argv)
{
  PA_Init();
  PA_InitVBL();
  PA_InitRand();
  
  PA_InitText(0,0);
  PA_InitText(1,0);
  
  s32 cpuWin = 0;
  s32 youWin = 0;
  s32 tie = 0;

  void MainMenu()
  {
    // Load backgrounds
    PA_EasyBgLoad(0,3,bg1_bottom);
    PA_EasyBgLoad(1,3,bg1_top);
  }
  
  void MainGame()
  {
    // Load backgrounds
    PA_EasyBgLoad(0,3,maingame_bottom);
    PA_EasyBgLoad(1,3,maingame_top);

    // Sprites for top screen
    PA_LoadSpritePal(1,0, (void*)sprites_Pal);
    PA_CreateSprite(1,0, (void*)qbutton_Sprite,OBJ_SIZE_64X64,1,0,31,87);
    PA_CreateSprite(1,1, (void*)qbutton_Sprite,OBJ_SIZE_64X64,1,0,159,87);
    PA_CreateSprite(1,2, (void*)indicate_blank_Sprite,OBJ_SIZE_64X32,1,0,31,159);
    PA_CreateSprite(1,3, (void*)indicate_blank_Sprite,OBJ_SIZE_64X32,1,0,159,159);
  
    // Sprites for bottom screen
    PA_LoadSpritePal(0,0, (void*)sprites_Pal);
    PA_CreateSprite(0,0, (void*)paper_Sprite,OBJ_SIZE_64X64,1,0,15,111);
    PA_CreateSprite(0,1, (void*)scissor_Sprite,OBJ_SIZE_64X64,1,0,95,111);
    PA_CreateSprite(0,2, (void*)rock_Sprite,OBJ_SIZE_64X64,1,0,175,111);
    
    PA_CreateSprite(0,3, (void*)blue_arrow_Sprite,OBJ_SIZE_32X32,1,0,31,63);
    PA_CreateSprite(0,4, (void*)blue_arrow_Sprite,OBJ_SIZE_32X32,1,0,111,63);
    PA_CreateSprite(0,5, (void*)blue_arrow_Sprite,OBJ_SIZE_32X32,1,0,191,63);

    PA_SetSpriteRotEnable(0,3,0);
    PA_SetSpriteRotEnable(0,4,0);
    PA_SetSpriteRotEnable(0,5,0);

    // SFX for top screen
    PA_SetSFXAlpha(1,7,15);
    PA_EnableSpecialFx(1,SFX_ALPHA,0,SFX_BG0 | SFX_BG1 | SFX_BG2 | SFX_BG3 | SFX_BD);

    // SFX for bottom screen
    PA_SetSFXAlpha(0,7,15);
    PA_EnableSpecialFx(0,SFX_ALPHA,0,SFX_BG0 | SFX_BG1 | SFX_BG2 | SFX_BG3 | SFX_BD);
  } // end MainGame
  
  void PlayerWins()
  {
    youWin = youWin + 1;
    PA_UpdateSpriteGfx(1,2, (void*)indicate_winner_Sprite);
    PA_UpdateSpriteGfx(1,3, (void*)indicate_blank_Sprite);
  }
	
  void CpuWins()
  {
    cpuWin = cpuWin + 1;
    PA_UpdateSpriteGfx(1,2, (void*)indicate_blank_Sprite);
    PA_UpdateSpriteGfx(1,3, (void*)indicate_winner_Sprite);
  }

  void PlayGame(s32 user)
  {
    switch (user)
    {
      case 1:
        PA_UpdateSpriteGfx(1,0, (void*)paper_Sprite);
        PA_SetSpriteMode(0,0,0);
        PA_SetSpriteMode(0,1,1);
        PA_SetSpriteMode(0,2,1);
        PA_SetSpriteMode(0,3,0);
        PA_SetSpriteMode(0,4,1);
        PA_SetSpriteMode(0,5,1);
        break;
      case 2:
        PA_UpdateSpriteGfx(1,0, (void*)scissor_Sprite);
        PA_SetSpriteMode(0,0,1);
        PA_SetSpriteMode(0,1,0);
        PA_SetSpriteMode(0,2,1);
        PA_SetSpriteMode(0,3,1);
        PA_SetSpriteMode(0,4,0);
        PA_SetSpriteMode(0,5,1);
        break;
      case 3:
        PA_UpdateSpriteGfx(1,0, (void*)rock_Sprite);
        PA_SetSpriteMode(0,0,1);
        PA_SetSpriteMode(0,1,1);
        PA_SetSpriteMode(0,2,0);
        PA_SetSpriteMode(0,3,1);
        PA_SetSpriteMode(0,4,1);
        PA_SetSpriteMode(0,5,0);
        break;
    }
		
    s32 cpu = PA_RandMinMax(1,3);

    switch (cpu)
    {
      case 1:
        PA_UpdateSpriteGfx(1,1, (void*)paper_Sprite);
        break;
      case 2:
        PA_UpdateSpriteGfx(1,1, (void*)scissor_Sprite);
        break;
      case 3:
        PA_UpdateSpriteGfx(1,1, (void*)rock_Sprite);
        break;
    }
		
    if (user == cpu) {
      tie = tie + 1;
      PA_UpdateSpriteGfx(1,2, (void*)indicate_tie_Sprite);
      PA_UpdateSpriteGfx(1,3, (void*)indicate_tie_Sprite);
    } else if ((user == 1 && cpu == 3) || (user == 2 && cpu == 1) || (user == 3 && cpu == 2)) {
      PlayerWins();
    } else if ((user == 1 && cpu == 2) || (user == 2 && cpu == 3) || (user == 3 && cpu == 1)) {
      CpuWins();
    }
  } // end PlayGame()

  bool inMainMenu = true;
  bool inMainGame = false;

  u16 zoom = 256;
  s16 zoom_mod = 2;
  
  MainMenu();

  // Infinite loop to keep the program running
  while (1)
  {
    inMainMenu = true;
    while (inMainMenu)
    {
      if (Pad.Newpress.Start) {
        inMainMenu = false;
        MainGame();
      }
      
      PA_WaitForVBL();
    } // end while(inMainMenu)
    
    inMainGame = true;
    while (inMainGame)
    {
      PA_SetRotsetNoAngle(0,0,zoom,zoom);
      zoom = zoom + zoom_mod;
      if (zoom == 320) {
        zoom_mod = -2;
      } else if (zoom == 256) {
        zoom_mod = 2;
      }

      PA_SetTextTileCol(0,9);
      PA_OutputText(0,21,2,"You: %d", youWin);
      PA_OutputText(0,21,3,"Cpu: %d", cpuWin);
      PA_OutputText(0,21,4,"Tie: %d", tie);
      
      if (Pad.Newpress.X || Pad.Newpress.Up || (Stylus.Released && Stylus.X > 15 && Stylus.X < 79 && Stylus.Y > 111 && Stylus.Y < 175)) {
        PlayGame(1);
      } else if (Pad.Newpress.A || Pad.Newpress.Right || (Stylus.Released && Stylus.X > 95 && Stylus.X < 159 && Stylus.Y > 111 && Stylus.Y < 175)) {
        PlayGame(2);
      } else if (Pad.Newpress.B || Pad.Newpress.Down || (Stylus.Released && Stylus.X > 175 && Stylus.X < 239 && Stylus.Y > 111 && Stylus.Y < 175)) {
        PlayGame(3);
      }
  
      if (Pad.Newpress.Start) {
        inMainGame = false;
        PA_DeleteSprite(0,0);
        PA_DeleteSprite(0,1);
        PA_DeleteSprite(0,2);
        PA_DeleteSprite(0,3);
        PA_DeleteSprite(0,4);
        PA_DeleteSprite(0,5);
        PA_DeleteSprite(1,0);
        PA_DeleteSprite(1,1);
        PA_DeleteSprite(1,2);
        PA_DeleteSprite(1,3);
        MainMenu();
      }

      if (Pad.Newpress.Select) {
        youWin = 0;
        cpuWin = 0;
        tie = 0;
      }
      
      PA_WaitForVBL();
    } // End while(inMainGame)
  }
	
  return 0;
} // End of main()
